package
{
	import fl.controls.Button;
	import fl.controls.TextInput;
	import flash.display.Sprite;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	import flash.events.NetStatusEvent;
	import flash.events.MouseEvent;
	import flash.events.Event;
	//import flash.net.ObjectEncoding;
	import flash.media.Video;

	public class MinPlay extends Sprite
	{
		private var nc:NetConnection;

		private var ns:NetStream;

		private var rtmpNow:String;

		private var msg:Boolean;

		private var vid1:Video;

		private var playBtn:Button;

		private var stopBtn:Button;

		private var textInput:TextInput;

		private var metaSniffer:Object;

		private var dur:Number;

		public function MinPlay()
		{
			//NetConnection.defaultObjectEncoding = flash.net.ObjectEncoding.AMF0;
			nc = new NetConnection();
			nc.addEventListener(NetStatusEvent.NET_STATUS, checkConnect);
			rtmpNow = "rtmp://192.168.1.209/livepkgrTest";
			//rtmpNow="rtmp:/vid2/recordings";
			nc.connect(rtmpNow);
			addMedia();
			addUI();
			playBtn.addEventListener(MouseEvent.CLICK, startPlay);
			stopBtn.addEventListener(MouseEvent.CLICK, stopPlay);
		}

		private function addMedia():void
		{
			vid1 = new Video(240, 180);
			addChild(vid1);
			vid1.x = 100;
			vid1.y = 50;
		}

		private function addUI():void
		{
			playBtn = new Button();
			playBtn.label = "Play";
			playBtn.x = 100;
			playBtn.y = 180 + 55;
			playBtn.width = 60;
			addChild(playBtn);
			stopBtn = new Button();
			stopBtn.label = "Stop Playing";
			stopBtn.width = 80;
			stopBtn.x = playBtn.x + 180;
			stopBtn.y = playBtn.y;
			addChild(stopBtn);
			textInput = new TextInput();
			textInput.x = playBtn.x;
			textInput.y = playBtn.y + 30;
			addChild(textInput);
		}

		private function checkConnect(e:NetStatusEvent):void
		{
			msg = (e.info.code == "NetConnection.Connect.Success");
			if (msg)
			{
				ns = new NetStream(nc);
				metaSniffer = new Object();
				ns.client = metaSniffer;
				metaSniffer.onMetaData = getMeta;
			}
		}

		private function getMeta(mdata:Object):void
		{
			for (var prop:Object in mdata)
			{
				trace(prop + " = " + mdata[prop]);
			}
		}

		private function startPlay(e:Event):void
		{
			if (ns)
			{
				playBtn.label = "Playing";
				vid1.attachNetStream(ns);
				ns.play(textInput.text);
			}
		}

		private function stopPlay(e:Event):void
		{
			playBtn.label = "Play";
			ns.play(false);
			ns.close();
		}
	}
}
